package day09demo2;

public class StudentTest3 {
    public static void main(String[] args) {
        Student[] arr = new Student[3];
        Student s1 = new Student(1, "日富美", 18);
        Student s2 = new Student(2, "花子", 19);
        Student s3 = new Student(3, "小春", 16);
        arr[0] = s1;
        arr[1] = s2;
        arr[2] = s3;
        int index = getIndex(arr,3);
        if (index >= 0 ){
            Student stu = arr[index];
            int newAge = stu.getAge() + 1;
            stu.setAge(newAge);
            printArr(arr);
        }else {
            System.out.println("当前id不存在，删除失败");
        }
    }

    public static int getIndex(Student[] arr,int id){
        for (int i = 0; i < arr.length; i++) {
            Student s = arr[i];
            if (s != null){
                int sid = s.getId();
                if (sid == id){
                    return i;
                }
            }
        }
        return -1;
    }

    public static void printArr(Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            Student s = arr[i];
            if(s != null){
                System.out.println(s.getId()+","+s.getName()+","+s.getAge());
            }
        }
    }

}
